You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Heartbeat every open AMQP connection, not just the last one used - #30
A process with more than one AMQP connection only heartbeats the connection it touched last. _getConnection() re-registers the single PCNTLHeartbeatSender on every call, and SIGALRM has one handler per process, so another provider's sender replaces it too. Every other connection goes quiet when idle, and the broker closes it for missed heartbeats. In production one worker deployment accounted for 548 of ~620 of these closes in three hours. A long-running consumer died with Connection reset by peer after its publisher connection was blocked by a memory alarm.
Idle connection on a provider that used another connection mode since: stays open.
Idle connection on a provider created before another provider in the same process: stays open.
Idle connection while other connections open and close faster than the heartbeat interval: stays open.
Disconnecting one connection mode: the provider's other connections keep heartbeating.
Push or consume after a background heartbeat failed (e.g. a long process pause): reconnects instead of throwing AMQPConnectionClosedException.
Consumer callback paused past the broker heartbeat timeout: its ack is skipped, consume() returns false, and the message is redelivered.
Consume connection lost part way through batchConsume(): the batch callback gets no deliveries from the lost connection; they arrive in the next batch.
AMQP_WITHOUT_SIGNALS defined true: no SIGALRM handler is installed, as before.
Released provider that has consumed: garbage collected; the registry holds connections by weak reference.
Test plan
Each behaviour has a test that was pushed alone first and failed in CI: first two with Missed server heartbeat, the six from review in three runs (1, 2, 3), each on the defect it covers.
With the fixes, all 42 jobs pass (PHP 7.4–8.4 × RabbitMQ 3.10–4.2).
A process holding more than one AMQP connection, from one provider's
connection modes or from several providers, should keep every connection
alive while it is idle. These tests fail: only the connection touched last
has a heartbeat sender, so the others miss their heartbeats.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
AmqpHeartbeat holds every open connection in the process and, on each
alarm, sends a heartbeat on any that has been quiet for half its heartbeat
interval and is not mid-write. A provider registers a connection when it
opens it and unregisters it when it disconnects that connection mode.
This replaces the per-provider PCNTLHeartbeatSender, which was re-registered
on every _getConnection() call and shared the process's single SIGALRM
handler with every other provider, so only the last connection touched was
heartbeated. The protected $_heartbeatSender property is removed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Connection churn can indefinitely postpone heartbeats, while swallowed heartbeat failures allow dead connections to be reused, causing broker timeouts and publish failures.
Updates the AMQP queue provider to use one process-wide heartbeat sender, aiming to keep idle connections alive across connection modes and provider instances.
Changes:
Adds a shared heartbeat registry and SIGALRM handler.
Registers connections when opened and unregisters them individually on disconnect.
Adds regression tests for idle connections across modes and providers.
File
Description
tests/Provider/Mock/AmqpMockProvider.php
Updates heartbeat controls and exposes existing connections for assertions.
tests/Provider/AmqpTest.php
Adds multi-connection and multi-provider heartbeat tests.
src/Provider/Amqp/AmqpQueueProvider.php
Integrates shared heartbeat registration with connection lifecycle management.
src/Provider/Amqp/AmqpHeartbeat.php
Adds the process-wide connection registry and heartbeat scheduler.
- An idle connection stays alive while other connections open and close
more often than the heartbeat interval.
- A push after a process pause long enough to miss the broker heartbeat
reconnects instead of writing to the closed socket.
- A provider that has consumed is garbage collected once released.
- AMQP_WITHOUT_SIGNALS leaves the SIGALRM handler untouched.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Hold connections by WeakReference, so a released provider (which a
consumer channel references through its callback) can be collected.
- Only ever bring the pending alarm forward. Registering or removing a
connection no longer postpones it, so churn cannot starve idle
connections of heartbeats.
- When a heartbeat fails inside the signal handler, flag the connection.
The provider disconnects it on its next push or consume and reconnects.
- Honour AMQP_WITHOUT_SIGNALS, as PCNTLHeartbeatSender did.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PHP runs the destructors of a garbage cycle on the first collection and
frees the cycle on the next one.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
A missed-heartbeat socket closure can still cause acknowledgement exceptions to escape to the worker, and regression coverage is missing for paused callbacks and disconnecting one mode while preserving another.
- A consumer callback paused past the broker heartbeat timeout should
leave its message for redelivery: its ack must not throw, consume()
returns false, and the next consume receives the message again.
- Disconnecting one connection mode keeps the provider's other
connection heartbeating.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A heartbeat that fails in the signal handler during a consumer callback
closes the socket. ack, nack and batchAck now skip settling on a flagged
consume connection, since its delivery tags are void and replaying them on
a new channel would settle unrelated messages. consume() then disconnects
and returns false, and the broker redelivers the messages.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Heartbeat-failure disconnects leave pending batch deliveries intact, allowing stale tags to be acknowledged on a replacement channel and potentially settling unrelated deliveries.
Handle connection-closed exception after missed heartbeat
src/Provider/Amqp/AmqpQueueProvider.php:309
If SIGALRM detects a missed heartbeat while consume() is waiting for a message, the resumed read can throw AMQPConnectionClosedException (upstream StreamIO, also verified in v3.7.3). Neither existing catch handles it, so execution skips this failure check and the exception escapes to the worker. Catch this exception when the consume connection is flagged so the disconnect-and-return-false path runs, while rethrowing unrelated failures.
A heartbeat failure part way through batchConsume() replaces the consume
connection. The batch should then hold only deliveries from the new
connection, not the lost delivery whose tag is void.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SIGALRM from the shared heartbeat handler interrupts sleep(), and its
whole-second remainder can lose part of a second per interruption, so a
12s idle could fall under the 9s missed-heartbeat threshold. This made
testHeartbeatAfterMissed fail intermittently. Every test now sleeps to a
microtime deadline.
When a heartbeat fails during the first delivery of a batch, consume()
returns false and batchConsume() hands the stale delivery to the callback.
The test now expects no stale delivery in that batch and the redelivery
in the next.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Deliveries collected by batchConsume() carry tags of the channel they
arrived on. Once that connection is gone the broker requeues them, so the
batch callback must not see them and batchAck() must not settle their tags
on a replacement channel.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Human assessment is needed because an unpublished critical finding identifies heartbeat-failure paths that can terminate the worker or leave earlier batch deliveries available after a timeout.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A process with more than one AMQP connection only heartbeats the connection it touched last.
_getConnection()re-registers the singlePCNTLHeartbeatSenderon every call, andSIGALRMhas one handler per process, so another provider's sender replaces it too. Every other connection goes quiet when idle, and the broker closes it for missed heartbeats. In production one worker deployment accounted for 548 of ~620 of these closes in three hours. A long-running consumer died withConnection reset by peerafter its publisher connection was blocked by a memory alarm.AMQPConnectionClosedException.consume()returnsfalse, and the message is redelivered.batchConsume(): the batch callback gets no deliveries from the lost connection; they arrive in the next batch.AMQP_WITHOUT_SIGNALSdefined true: noSIGALRMhandler is installed, as before.Test plan
Missed server heartbeat, the six from review in three runs (1, 2, 3), each on the defect it covers.🤖 Generated with Claude Code